< previous page page_128 next page >

Page 128
copied and pasted is hard coded into the routine. To make this a truly generic routine, you can use a property of the Form object. The ActiveControl property gives you access to whatever control is active on the form at that moment. When creating a copy-and-paste routine you must identify the type of data you are copying, because the Clipboard object handles text and graphics differently. Also, when users choose to paste the data from the clipboard, you must make sure they are pasting the data to a field that has the same data type of the data that was copied. In addition, as a rule you should empty the clipboard before copying any data into it. This entire process uses several different methods and properties of the Clipboard, Form, and Control objects. The code in Listing 5.1 shows the generic Copy/Paste routines that you can add to your application code.
LISTING 5.1. COPY_PASTE.TXTCHECKING THE DATA TYPE BEFORE COPYING AND PASTING DATA USING THE CLIPBOARD.

8694cbadb9b1ef7075903f6f95fc1db2.gif
 1: Private Sub mnuEditCopy_Click()
 2:     Clipboard.Clear
 3:     If TypeOf Form1.ActiveControl Is TextBox Then
 4:         Clipboard.SetText Form1.ActiveControl
 5:     ElseIf TypeOf Form1.ActiveControl Is PictureBox Then
 6:         Clipboard.SetData Form1.ActiveControl
 7:     End If
 8: End Sub
 9:
10: Private Sub mnuEditPaste_Click()
11:     If TypeOf Form1.ActiveControl Is TextBox Then
12:         If Clipboard.GetFormat(vbCFText) Then
13:             Form1.ActiveControl = Clipboard.GetText
14:         Else
15:             MsgBox Invalid data in clipboard, vbInformation, App.Title
16:         End If
17:     ElseIf TypeOf Form1.ActiveControl Is PictureBox Then
18:         If Clipboard.GetFormat(vbCFBitmap) Then
19:             Form1.ActiveControl = Clipboard.GetData
20:         Else
21:             MsgBox Invalid data in clipboard, vbInformation, App.Title
22:         End If
23:     End If
24: End Sub.

Accessing Objects
Accessing or working with Visual Basic objects is really quite simple when you use the available statements, properties, and methods. In the previous section, you saw how to

 
< previous page page_128 next page >

If you like this book, buy it!